<template>
  <div>
    <a-card v-if="showRight" :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('property:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <!-- <a-col :md="8" :sm="24">
              <a-form-item label="所属概念">
                <a-select style="width: 100%" v-model="queryParam.schemaId" placeholder="请选择所属概念">
                  <a-select-option v-for="(item,index) in schemaIdData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
            <a-col :md="8" :sm="24">
              <a-form-item label="属性名：">
                <a-input v-model="queryParam.keywords" allow-clear placeholder="请输入属性名称或英文名" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" :bodyStyle="contentStyle()">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :showRightTool="showRight"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('property:add') && showRight">
          <a-button type="primary" v-if="hasPerm('property:add')" icon="plus" @click="$refs.addForm.add(queryParam)">
            新增属性
          </a-button>
        </template>
        <span slot="datetypescopedSlots" slot-scope="text">
          {{ 'data_type' | dictType(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('property:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('property:edit') & hasPerm('property:delete')" />
          <a-popconfirm
            v-if="hasPerm('property:delete')"
            placement="topRight"
            title="确认删除？"
            @confirm="() => propertyDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form :queryParam="queryParam" ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
import { STable } from '@/components'
import { propertyPage, propertyDelete } from '@/api/modular/main/knowl/property/propertyManage'
import addForm from './addForm.vue'
import editForm from './editForm.vue'

export default {
  components: {
    STable,
    addForm,
    editForm
  },
  props: {
    queryParam: {
      type: Object,
      default: function() {
        return {}
      }
    },
    modal: {
      type: Boolean,
      default: function() {
        return false
      }
    }
  },
  data() {
    return {
      showRight: true,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      // queryParam: {},
      // 表头
      columns: [
        {
          title: '属性名称',
          align: 'center',
          dataIndex: 'chName',
          width: '20%'
        },
        {
          title: '英文名',
          align: 'center',
          dataIndex: 'enName',
          width: '20%'
        },
        {
          title: '',
          align: 'center',
          dataIndex: 'modifyNum'
        },
        {
          title: '数据类型',
          align: 'center',
          dataIndex: 'datetype',
          scopedSlots: { customRender: 'datetypescopedSlots' }
        },
        {
          title: '所属概念',
          align: 'center',
          dataIndex: 'schemaName',
          scopedSlots: { customRender: 'schemaIdscopedSlots' },
          width: '22%'
        }
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        console.log(parameter)
        return propertyPage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      schemaIdData: [],
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created() {
    if (this.hasPerm('property:edit') || this.hasPerm('property:delete')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
    const schemaIdOption = this.$options
    this.schemaIdData = schemaIdOption.filters['dictData']('common_status')
  },
  methods: {
    contentStyle() {
      if (this.showRight) {
        return { padding: 24 }
      }
      return { 'padding': '0 6px 0 0' }
    },
    showRightTool(show) {
      this.showRight = show
      const action = this.columns.find((e) => {
        return e.title.includes('操作')
      })
      if (!show && action) {
        this.columns.splice(this.columns.length - 1)
      }
    },
    tableReload() {
      this.handleOk()
    },
    propertyDelete(record) {
      propertyDelete(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
</style>
